package mobi.hami.huapin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import mobi.hami.huapin.base.Result;
import mobi.hami.huapin.constants.BaseEnums;
import mobi.hami.huapin.dto.UserNoteLogDTO;
import mobi.hami.huapin.model.UserShareLog;
import mobi.hami.huapin.service.UserNoteLogService;
import mobi.hami.huapin.service.UserNoteService;
import mobi.hami.huapin.service.UserShareLogService;
import mobi.hami.huapin.util.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * Create by 小峰 on 2018/9/14.
 * 纸条说页面
 */
@RestController
@RequestMapping(value = "/api/usernote", produces = "application/json")
@Api(description = "纸条说页面处理")
public class UserNoteLogController {

    @Autowired
    private UserNoteService userNoteService;
    @Autowired
    private UserNoteLogService userNoteLogService;
    @Autowired
    private UserShareLogService userShareLogService;

    @RequestMapping(value = "/getnoteenter.do", method = RequestMethod.GET)
    @ApiOperation(value = "根据空间Id获取纸条的入口信息", notes = "空间id")
    public Result getNoteEnter(Integer spaceId) {
        return Results.successWithData(userNoteService.getNoteEnterBySpaceId(spaceId), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/getyestusernum.do", method = RequestMethod.GET)
    @ApiOperation(value = "根据空间Id获取昨天新增加的人数", notes = "空间id")
    public Result getYesterDayNewUserCout(Integer spaceId) {
        return Results.successWithData(userNoteService.getYesterDayNewUserBySpaceId(spaceId), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/getotherusernotelogcount.do", method = RequestMethod.GET)
    @ApiOperation(value = "根据空间Id获取其它人员及气泡数", notes = "空间id和用户id")
    @ApiResponses({
            @ApiResponse(code = 200, message = "user_id:用户id,user_name:用户名称,user_head:用户头像,rownum:排序,note_count:气泡数")
    })
    public Result getOtherUserNoteLogCount(Integer spaceId, Integer userId) {
        return Results.successWithData(userNoteLogService.getUserNoteBySpaceIdAndUserId(spaceId, userId), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }


    @RequestMapping(value = "/getcurrentusersum.do", method = RequestMethod.GET)
    @ApiOperation(value = "根据空间Id,用户Id，获取当前的被贴纸条数", notes = "空间id,用户id")
    public Result getCurrentUserCount(Integer spaceId, Integer userId) {
        return Results.successWithData(userNoteLogService.getUserNoteLogCount(spaceId, userId), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/getuserreplysum.do", method = RequestMethod.GET)
    @ApiOperation(value = "根据空间Id,用户Id，获取当前贴出的纸条被回复的个数", notes = "空间id,用户id")
    public Result getUserReplyCount(Integer spaceId, Integer userId) {
        return Results.successWithData(userNoteLogService.getUserReplyCount(spaceId, userId), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/getuserbynotesum.do", method = RequestMethod.GET)
    @ApiOperation(value = "根据空间Id,用户Id，获取当前用户被贴的总数及当天被贴数", notes = "空间id,用户id")
    public Result getUserByNoteCount(Integer spaceId, Integer userId) {
        return Results.successWithData(userNoteLogService.getUserByNote(spaceId, userId), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/getusernotelogbypage.do", method = RequestMethod.POST)
    @ApiOperation(value = "获取用户被贴纸条的List(分页)", notes = "{shareId:分享的id," +
            "searchUserId:需要查询用户Id,currentUserId:当前登录的用户Id,spaceId:空间Id,pageIndex:1,pageSize:10}")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{startPageNum:开始页数,totalNum:总条数,pageSize:每页显示条数," +
                    "totalPageNum:总页数,List[{id:被贴纸条记录id,space_id:空间Id,to_user_id:被贴用户Id,from_user_id:贴出用户Id," +
                    "note_id:纸条Id,note_comtent:纸条内容,create_time:创建时间,thumbup_counts:点赞数,reply_time:回复时间," +
                    "headImage:头像地址(请使用mockHeadImage),is_reply:(1 新增，2表示滚，4表示懂我),thumbup:当前用户点赞(0:未点1:已点)}]}")
    })
    public Result getUserNoteLogByPage(@RequestBody Map<String, Object> map) {
        Map<String, Object> resultMap = userNoteLogService.getUserNoteLogByPage(map);
        return Results.successWithData(resultMap, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/getuerreplyrat.do", method = RequestMethod.GET)
    @ApiOperation(value = "获取用户回复率（小程序）", notes = "用户id")
    public Result getUserReplyRat(Integer userId) {
        return Results.successWithData(userNoteLogService.getUserReplyRat(userId), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/getnewusernotelog.do", method = RequestMethod.GET)
    @ApiOperation(value = "获取用户最新被贴的纸条", notes = "用户id,空间Id")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{id:被贴纸条记录id,space_id:空间Id,to_user_id:被贴用户Id,from_user_id:贴出用户Id," +
                    "note_id:纸条Id,note_comtent:纸条内容,create_time:创建时间,thumbup_counts:点赞数,reply_time:回复时间," +
                    "headImage:头像地址,is_reply:(1 新增，2表示滚，4表示懂我),thumbup:当前用户点赞(0:未点1:已点)}")
    })
    public Result getNewUserNoteLog(Integer userId, Integer spaceId, HttpServletRequest request) {
        UserNoteLogDTO userNoteLogDTO = userNoteLogService.findModelByModel(userId, spaceId);
        if (userNoteLogDTO != null) {
            userNoteLogDTO.setHeadImage("http://mini.hami.mobi/images/huapin/config/twothree2.png");
            return Results.successWithData(userNoteLogDTO, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
        }
        return Results.successWithData(BaseEnums.NOT_FOUND.code(), BaseEnums.NOT_FOUND.desc());
    }

    @RequestMapping(value = "/updateusernotereplystatus.do", method = RequestMethod.GET)
    @ApiOperation(value = "保存纸条的回复内容", notes = "纸条记录id,回复状态Id(2表示滚，4表示懂我)")
    public Result updateUserNoteLogReplyStatus(Integer id, Integer replyStatus) {
        return Results.successWithData(userNoteLogService.updateUserNoteLogReply(id, replyStatus), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/getreplyusernoteloglist.do", method = RequestMethod.POST)
    @ApiOperation(value = "获取被贴出的纸条,回复列表", notes = "{userId:当前用户id," +
            "spaceId:空间Id,pageIndex:1,pageSize:10}")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{startPageNum:开始页数,totalNum:总条数,pageSize:每页显示条数," +
                    "totalPageNum:总页数,List[{id:被贴纸条记录id,space_id:空间Id,to_user_id:被贴用户Id,from_user_id:贴出用户Id," +
                    "note_id:纸条Id,note_comtent:纸条内容,create_time:创建时间,thumbup_counts:点赞数,reply_time:回复时间," +
                    "headImage:头像地址,is_reply:(1 新增，2表示滚，4表示懂我),thumbup:当前用户点赞(0:未点1:已点)}]}")
    })
    public Result getReplyUserNoteLogList(@RequestBody Map<String, Object> mapRequest) {
        return Results.successWithData(userNoteLogService.getReplyUserNoteLogList(mapRequest), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/getcurrentdaynotelogsum.do", method = RequestMethod.GET)
    @ApiOperation(value = "获取当天用户被贴纸条的个数", notes = "空间Id和用户id")
    public Result getCurrentDayNoteLogSum(Integer spaceId, Integer userId) {
        return Results.successWithData(userNoteLogService.getCurrentDayNoteLogSum(spaceId, userId), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/giveusernote.do", method = RequestMethod.POST)
    @ApiOperation(value = "给用户帖纸条", notes = "{to_user_id:被帖会员编号,from_user_id:贴纸条操作的用户会员编号," +
            "spaceid:空间编号,note_id:纸条id,note_comtent:纸条内容}")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{ data: true:成功,否则失败}")
    })
    public Result giveUserNote(@RequestBody Map<String, String> reqMap) {

        return userNoteLogService.giveUserNote(reqMap);

    }
    @RequestMapping(value = "/getnotestatus.do", method = RequestMethod.POST)
    @ApiOperation(value = "判断该用户是否贴了纸条", notes = "{to_user_id:被帖会员编号,from_usgetcurrentusersumer_id:贴纸条操作的用户会员编号," +
            "spaceid:空间编号,note_id:纸条id}")
    @ApiResponses({
            @ApiResponse(code = 200, message = "{ data: true:成功,否则失败}")
    })
    public Result getNoteStatus(@RequestBody Map<String, String> reqMap) {

        return userNoteLogService.getNoetStatus(reqMap);

    }
    @RequestMapping(value = "/adduserthubup.do", method = RequestMethod.GET)
    @ApiOperation(value = "用户点赞处理", notes = "空间Id,用户id,被贴纸条Id")
    public Result userThubup(Integer spaceId, Integer userId, Integer noteLogId) {
        return Results.successWithData(userNoteLogService.addThubupCountById(spaceId, userId, noteLogId), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/getusernotecount.do", method = RequestMethod.GET)
    @ApiOperation(value = "获取用户当前可用的纸条数", notes = "空间Id,用户id")
    public Result getUserNoteCount(Integer spaceId, Integer userId) {

        return Results.successWithData(userNoteService.getUserNoteCount(spaceId, userId), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/saveusersharelog.do", method = RequestMethod.POST)
    @ApiOperation(value = "更新分享记录,返回分享Id", notes = "{note_id:纸条Id,space_id:空间Id,user_id:分享者Id}")
    public Result saveUserShareLogInfo(@RequestBody UserShareLog userShareLog) {
        userShareLog.setGuide_num(0);
        userShareLog.setCreate_time(new Date());
        return Results.successWithData(userShareLogService.saveUserShareLog(userShareLog), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/updatesharelog.do", method = RequestMethod.POST)
    @ApiOperation(value = "更新分享的纸条信息", notes = "分享Id和纸条Id")
    public Result updateUserShareLogById(Integer shareId, Integer noteId) {
        UserShareLog userShareLog = new UserShareLog();
        userShareLog.setId(shareId);
        userShareLog.setNote_id(noteId);
        userShareLog.setShare_status(1);
        return Results.successWithData(userShareLogService.updateModel(userShareLog), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

}
